﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Jy123.BLL.Public;
using Jy123.Model;

namespace Jy123.BLL.User
{
    public class Blog
    {
        /// <summary>
        /// 我的好友的日志列表
        /// </summary>
        /// <param name="userID">用户的ID</param>
        /// <param name="count">需获取的记录数量</param>
        /// <returns>数据集</returns>
        //public DataSet BlogList_MyFriend(int userID, int count)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select TOP(@Count) * from V_User_BlogList ");
        //    strSql.Append("where (IsLock=0 and FUser in (select FFriend from User_Friend where FUser=@UserID) and FUser!=@UserID and Privacy<>3) ");
        //    strSql.Append("order by ID desc ");
        //    SqlParameter[] parameters = { 
        //                                new SqlParameter("@UserID", SqlDbType.Int, 4),
        //                                new SqlParameter("@Count", SqlDbType.Int, 4)};
        //    parameters[0].Value = userID;
        //    parameters[1].Value = count;
        //    return DBUtility.DbHelperSQL.Query(strSql.ToString(), parameters);
        //}

        /// <summary>
        /// 我的好友的日志列表
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="userid">用户ID</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public DataSet BlogList_MyFriend(int cpage, int psize, int userid, out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = "(IsLock=0 and FUser in (select FFriend from User_Friend where FUser='" + userid + "') and FUser!='" + userid + "' and Privacy<>3)",
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                //Tables = "V_User_BlogList",//每个好友所有的日志
                Tables = "V_User_BlogList_Friend",//取每个好友的最后一篇日志
                Sort = "ID desc"
            }, out counts);
        }

        /// <summary>
        /// 我的日志列表
        /// </summary>
        /// <param name="userID">用户的ID</param>
        /// <param name="count">需获取的记录数量</param>
        /// <returns>数据集</returns>
        //public DataSet BlogList_My(int userID, int count)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select TOP(@Count) * from V_User_BlogList ");
        //    strSql.Append("where (IsLock=0  and FUser=@UserID and Privacy<>@UserID) ");
        //    strSql.Append("order by ID desc ");
        //    SqlParameter[] parameters = { 
        //                                new SqlParameter("@UserID", SqlDbType.Int, 4),
        //                                new SqlParameter("@Count", SqlDbType.Int, 4)};
        //    parameters[0].Value = userID;
        //    parameters[1].Value = count;
        //    return DBUtility.DbHelperSQL.Query(strSql.ToString(), parameters);
        //}
        /// <summary>
        /// 我的日志列表
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="userid">用户ID</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public DataSet BlogList_My(int cpage, int psize, int userid, out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = "(IsLock=0  and FUser='"+userid+"' and Privacy<>'"+userid+"')",
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Tables = "V_User_BlogList",
                Sort = "ID desc"
            }, out counts);
        }

        /// <summary>
        /// 我和我的好友的日志列表
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="userid">用户ID</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public DataSet BlogList_MyAndFriend(int cpage, int psize, int userid, out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = "(IsLock=0 and (FUser in (select FFriend from User_Friend where FUser='" + userid + "') or FUser='" + userid + "') and Privacy<>'" + userid + "')",
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Tables = "V_User_BlogList",
                Sort = "ID desc"
            }, out counts);
        }
        //V_Manage_User_BlogList


        /// <summary>
        /// 后台管理日志列表
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public DataSet ManageBlogList(int cpage, int psize, string strWhere,string strSort, out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Tables = "V_Manage_User_BlogList",
                Sort = strSort
            }, out counts);
        }
        /// <summary>
        /// 批量删除日志
        /// </summary>
        /// <param name="ids">日志ID集，逗号隔开</param>
        /// <returns>受影响行数</returns>
        public int DelBlogs(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            string sql = "delete User_Blog where id in ({0})";
            StringBuilder strid = new StringBuilder();
            string[] idsS = ids.Split(',');
            SqlParameter[] parameters = new SqlParameter[idsS.Length];
            int i = 0;
            foreach (string id in idsS)
            {
                strid.Append(string.Format("@id{0}", i));
                strid.Append(",");
                parameters[i] = new SqlParameter(string.Format("@id{0}", i), SqlDbType.Int, 4);
                parameters[i].Value = id;
                i++;
            }
            strSql.AppendFormat(sql, strid.ToString().Substring(0, strid.ToString().Length - 1));
            return DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 日志评论列表
        /// </summary>
        /// <param name="cpage">起始页</param>
        /// <param name="psize">页记录数量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <returns>数据集</returns>
        public DataSet BlogDetailComment(int cpage,int psize,string strWhere)
        {
            Jy123.Model.MPagination modelPag = new MPagination();
            modelPag.Tables = "V_User_BlogCommentList";
            modelPag.PrimaryKey = "ID";
            modelPag.CurrentPage = cpage;
            modelPag.PageSize = psize;
            modelPag.Fields = "*";
            modelPag.Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere;
            modelPag.Sort = "id asc";
            return Pagination.GetList(modelPag);
        }

        /// <summary>
        /// 统计用户拥有的日志数量
        /// </summary>
        /// <param name="UserID">用户IDs</param>
        /// <returns></returns>
        public int BlogCount(int UserID)
        {
            string sql = "select count(*)as num from User_Blog where FUser =@UserID";
            SqlParameter[] parameters = { 
                                        new SqlParameter("@UserID", SqlDbType.Int, 4)};
            parameters[0].Value = UserID;
            return (int)DBUtility.DbHelperSQL.GetSingle(sql, parameters);
        }
    }
}
